﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace SHomeWorkshop.LunarMind.Adorners
{
    public class TitleOutBorderAdorner : Adorner
    {
        // Be sure to call the base class constructor.
        /// <summary>
        /// 用以表示标题的外边框（圆角矩形，虚线）。
        /// </summary>
        /// <param name="adornedElement">应传入标题的“MainBorder”主子元素。</param>
        public TitleOutBorderAdorner(UIElement adornedElement, Title masterTitle)
            : base(adornedElement)
        {
            this.masterTitle = masterTitle;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Unspecified);
        }

        private Title masterTitle;

        public Title MasterTitle
        {
            get { return masterTitle; }
        }

        // A common way to implement an adorner's rendering behavior is to override the OnRender
        // method, which is called by the layout system as part of a rendering pass.
        protected override void OnRender(DrawingContext drawingContext)
        {
            if (this.masterTitle == null) return;

            Rect adornedElementRect;

            switch (Globals.MainWindow.View)
            {
                case ViewMode.HorizontalList:
                case ViewMode.VerticalList:
                    {
                        adornedElementRect = new Rect(-10, -8,
                                this.masterTitle.NeedSize.Width,
                                this.masterTitle.NeedSize.Height);
                        break;
                    }
                default:
                    {
                        if (masterTitle.Direction == TitleDirection.Right)
                        {
                            adornedElementRect = new Rect(-10,
                                -(this.masterTitle.NeedSize.Height - this.masterTitle.ActualHeight) / 2 - 8,
                                this.masterTitle.NeedSize.Width,
                                this.masterTitle.NeedSize.Height);
                        }
                        else
                        {
                            adornedElementRect = new Rect(this.masterTitle.ActualWidth - this.masterTitle.NeedSize.Width - 10,
                                -(this.masterTitle.NeedSize.Height - this.masterTitle.ActualHeight) / 2 - 8,
                                this.masterTitle.NeedSize.Width,
                                this.masterTitle.NeedSize.Height);
                        }
                        break;
                    }
            }

            #region 先画线。
            Pen linePen = null;

            linePen = new Pen(Brushes.DimGray, 2);
            //linePen = this.masterTitle.Editor.SubPanelSelectionAdornerPen;
            linePen.DashStyle = DashStyles.Dash;

            linePen.LineJoin = PenLineJoin.Round;
            linePen.StartLineCap = PenLineCap.Round;
            linePen.EndLineCap = PenLineCap.Round;

            double r = this.masterTitle.LowerLevelTitles.Count * 5;

            if (r < 5) r = 5;
            if (r > 20) r = 20;

            drawingContext.DrawRoundedRectangle(null, linePen, adornedElementRect, r, r);
            #endregion
        }
    }
}
